//
//  ByShopVC.m
//  hutouben_ios
//
//  Created by mc on 16/11/29.
//  Copyright © 2016年 mc. All rights reserved.
//

#import "ByShopVC.h"

// vc
#import "ByShopListVC.h"            // 列表页

@interface ByShopVC ()

@end

@implementation ByShopVC

- (void)viewDidLoad {
    [super viewDidLoad];

    
    [self createViews];
}

- (void)createViews {
    
    CGFloat x, y, width, height;
    CGFloat margin = 8;
    
    NSArray *images = @[@"fenlei_chaiyou.png",
                        @"fenlei_luntai",
                        @"fenlei_famen",
                        @"fenlei_qipei",
                        @"fenlei_niaosu"];
    NSArray *texts = @[@"柴油机油",
                       @"轮胎",
                       @"阀门",
                       @"汽配",
                       @"车用尿素",];
    for (int i = 0; i < texts.count; i ++) {
        width = (SCREEN_WIDTH - 3 * margin) / 2;
        height = width * 0.8;
        x = margin + (i % 2) * (width + margin);
        y = margin + (i / 2) * (height + margin);
        UIControl *view = [self createVerticalModeView:CGRectMake(x, y, width, height)
                                             imageSize:CGSizeMake(height * 0.4, height * 0.4)
                                             imageName:images[i]
                                              textFont:FONT(18)
                                                  text:texts[i]
                                                margin:10];
        view.layer.cornerRadius = 5;
        view.tag = 1 + i;
        [self.view addSubview:view];
        
        
        [view addTarget:self action:@selector(tapAction:) forControlEvents:UIControlEventTouchUpInside];
    }

}


// 创建垂直排版的视图（可点击）
- (UIControl *)createVerticalModeView:(CGRect)frame
                            imageSize:(CGSize)size
                            imageName:(NSString *)name
                             textFont:(UIFont *)font
                                 text:(NSString *)text
                               margin:(CGFloat)margin {
    
    // 1.创建基底视图
    UIControl *basalView = [[UIControl alloc] initWithFrame:frame];
    basalView.backgroundColor = [UIColor colorWithWhite:1 alpha:0.7];
    
    // 2.创建图片
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, size.width, size.height)];
    imageView.image = [UIImage imageNamed:name];
    [basalView addSubview:imageView];
    
    // 3.创建文字
    CGFloat fontSize = [font pointSize];
    CGFloat width = basalView.width;
    CGFloat height = [text getSizeOfString:fontSize].height;
    UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, width, height)];
    textLabel.adjustsFontSizeToFitWidth = YES;
    textLabel.font = font;
    textLabel.text = text;
    textLabel.textColor = TEXT_COLOR;
    textLabel.textAlignment = NSTextAlignmentCenter;
    [basalView addSubview:textLabel];
    
    // 4.适配距离
    CGFloat remine = basalView.height - imageView.height - textLabel.height - margin;
    imageView.top = remine / 2;
    imageView.x = basalView.width / 2;
    textLabel.top = imageView.bottom + margin;
    textLabel.x = basalView.width / 2;
    
    return basalView;
    
}

- (void)tapAction:(UIControl *)sender {
    
    ByShopListVC *vc = [[ByShopListVC alloc] init];
    [self by_normalPush:vc hasNavi:YES];
}

@end
